
export default class PostProcessUtil{
    constructor(viewer){
        this.viewer =viewer;
        this.stageList =[];
        this.featurePickerHandler =new Cesium.ScreenSpaceEventHandler(viewer.canvas);
        this.silhouetteGreen = Cesium.PostProcessStageLibrary.createEdgeDetectionStage();
        this.silhouetteGreen.uniforms.color = Cesium.Color.LIME;
        this.silhouetteGreen.uniforms.length = 0.01;
        this.silhouetteGreen.selected = [];
        let silhouetteStage = Cesium.PostProcessStageLibrary.createSilhouetteStage([this.silhouetteGreen]);
        this.viewer.scene.postProcessStages.add(
            silhouetteStage
        );
        this.event =new Cesium.Event();
       
    }
     bindEvent(callback){
        this.event.addEventListener(callback);
     }
     unbindEvent(callback){
         this.event.removeEventListener(callback);
     }
    removeFeaturePicker(){
        this.silhouetteGreen.selected =[];
        this.featurePickerHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
    }
    // annotate(movement,feature){
    //     if(this.viewer.scene.pickPositionSupported){
    //         let cartesian =this.viewer.scene.pickPosition(movement.posiiton);
    //         if(Cesium.defined(cartesian)){
    //             annotations.add({
    //                 position:cartesian,
    //                 showBackground:true,
    //                 font:'14px monospace',
    //                 horizontalOrigin:Cesium.HorizontalOrigin.LEFT,
    //                 verticalOrigin:Cesium.VerticalOrigin.BOTTOM,
    //                 disableDepthTestDistance:Number.POSITIVE_INFINITY,
    //             })
    //         }
    //     }
    // }
    loadFeaturePicker() {
        var viewer =this.viewer;
      // 清楚系统默认的鼠标左键事件
    //   viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
      var scene = viewer.scene;
      if (!scene.pickPositionSupported) {
          window.alert("This browser does not support pickPosition");
      }

      this.featurePickerHandler.setInputAction(movement => {
          var feature = scene.pick(movement.position);
          let cartographic =this.viewer.scene.globe.ellipsoid.cartesianToCartographic(scene.pickPosition(movement.position));
          let lng =Cesium.Math.toDegrees(cartographic.longitude);
          let lat =Cesium.Math.toDegrees(cartographic.latitude);
          console.log(cartographic);
          if (!Cesium.defined(feature)) {
              return;
          }
          if(feature.primitive instanceof Cesium.Model){
              console.log('model')
            }
            if(feature.primitive instanceof Cesium.Billboard){
                let data =feature.id.kml.extendedData;
                if(!data){
                    return;
                }   

                let properties ={
                    name:feature.id.name,
                    detail:data.detail&&data.detail.value,
                    photo:data.photo&&data.photo.value.split(';')
                }
                console.log(properties)
                this.event.raiseEvent({openDetail:true,properties:properties});
            }
          if (feature instanceof Cesium.Cesium3DTileFeature) {
              this.silhouetteGreen.selected = [feature];
              var propertyNames =feature.getPropertyNames();
              var length =propertyNames.length;
              console.log(feature);
            
            let propertyList =[]
              for(var i=0;i<length;i++){
                    propertyList.push({
                        'name': propertyNames[i],
                        'value': feature.getProperty(propertyNames[i])
                    })
                }
                this.event.raiseEvent({openRight:true,properties:propertyList});
                //   global.domUtil.createTable(propertyList)
                
                // global.domUtil.createHtmlOverlay({lng:lng,lat:lat},'hello');

                //   console.log(movement.position)
          }
  
      }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
  }
  //加载亮度处理栈
  loadBrightnessStage() {
      var viewer =this.viewer;
      var stages = viewer.scene.postProcessStages;
      var brightness = stages.add(Cesium.PostProcessStageLibrary.createBrightnessStage())
      this.stageList.push({index:0,name:"brightness"})
      this.setBrightness(1)
      return brightness
  }
   //设置亮度
  setBrightness(brightnessValue) {
      //获取亮度处理栈
      var stage =undefined;
      for(var i=0;i<this.stageList.length;i++){
          var item =this.stageList[i];
          if(item.name=="brightness"){
              stage =this.viewer.scene.postProcessStages.get(item.index)
              break;
          }
      }
      var brightness = stage;
      brightness.enabled = true;
      //更改亮度值
      brightness.uniforms.brightness = Number(brightnessValue);
  }

}